﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using zyconfig;

namespace zyui
{
    public class Reader
    {
        /// <summary>
        /// 是否存在外借超期，存在返回false否则返回true
        /// </summary>
        /// <param name="dztm"></param>
        /// <param name="errorMsg"></param>
        /// <returns></returns>
        public static bool checkOverdue(string dztm,out string errorMsg)
        {
            bool f = true;
            using (DbOperHandler _open = new Common().Doh())
            {
                _open.Reset();
                _open.ConditionExpress = "读者条码='" + dztm + "' and 应归还时间<'"+DateTime.Now+"'";
                if (_open.Exist("流通库"))
                {
                    f = false;
                    errorMsg = "该读者外借文献已超期！";
                }
                else
                {
                    f = true;
                    errorMsg = "";
                }
            }
            return f;
        }
        /// <summary>
        /// 获取读者该库室剩余可借数
        /// </summary>
        /// <param name="dztm">读者条码</param>
        /// <param name="xnks">虚拟库室</param>
        /// <returns></returns>
        public static int getkwj(string dztm,int xnks)
        {
            int kwj = 0;
            using (DbOperHandler _open = new Common().Doh())
            {
                _open.Reset();
                _open.ConditionExpress = "读者条码='" + dztm + "'";
                object[] obj = _open.GetFields("读者库", "单位代码,级别代码");
                int ywj = 0;
                _open.Reset();
                _open.ConditionExpress = "读者条码='" + dztm + "' and 虚拟库室="+xnks;
                DataTable dt = _open.GetGroupCount("流通库", "虚拟库室", "虚拟库室", "DESC");
                foreach(DataRow drow in dt.Rows)
                {
                    ywj = Convert.ToInt32(drow["count"]);
                }
                _open.Reset();
                _open.ConditionExpress = "单位代码='" + obj[0] + "' and 级别代码='" + obj[1]+"' and 虚拟库室="+xnks;
                int dkj = Convert.ToInt32(_open.GetField("流通参数定义", "可外借数"));
                kwj = dkj - ywj;
            }
            return kwj;
        }
        /// <summary>
        /// 判断读者是否可借该文献
        /// </summary>
        /// <param name="dztm"></param>
        /// <param name="xnks"></param>
        /// <returns></returns>
        public static bool getBkwj(string dztm, int xnks, out string errorMsg)
        {
            bool f = false;errorMsg = "";
            using (DbOperHandler _open = new Common().Doh())
            {
                _open.Reset();
                _open.ConditionExpress = "读者条码='" + dztm + "'";
                object[] obj = _open.GetFields("读者库", "单位代码,级别代码");
                _open.Reset();
                _open.ConditionExpress = "单位代码='" + obj[0] + "' and 级别代码='" + obj[1] + "' and 虚拟库室=" + xnks;
                if (_open.Exist("流通参数定义"))
                {
                    int kwjs = Convert.ToInt32(_open.GetField("流通参数定义", "可外借数"));
                    if (kwjs > 0)
                        f = true;
                    else
                    {
                        f = false;
                        errorMsg = "读者不可借阅该库室的文献！";
                    }
                }
                else
                {
                    f = false;
                    errorMsg = "读者不可借阅该库室的文献！";
                }
            }
            return f;
        }
    }
}
